package com.lanou3g.zjl.chating.home.setting;

import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupMenu;
import android.widget.TextView;
import android.widget.Toast;

import com.easemob.chat.EMContactManager;
import com.easemob.exceptions.EaseMobException;
import com.lanou3g.zjl.chating.R;
import com.lanou3g.zjl.chating.base.BaseActivity;
import com.lanou3g.zjl.chating.home.personal_center.CircleImageView;
import com.lanou3g.zjl.chating.utils.GetUserAvatarHelper;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnItemLongClick;

/**
 * Created by dllo on 15/11/30.
 */
public class ContactBlackListActivity extends BaseActivity {
    private List<String> blackList;//用户黑名单
    private ListAdapter adapter;
    private Handler handler;
    @Bind(R.id.groupBlackListView)
    ListView groupBlackListView;

    @Override
    protected int getLayout() {

        return R.layout.group_black_list;
    }

    @OnClick(R.id.finishGroupBalckList)
    void backToSetting() {
        finish();
    }

    @Override
    protected void initData() {
        getAsynk();
        getBlackList();
        adapter = new ListAdapter(blackList);
        groupBlackListView.setAdapter(adapter);
    }

    //获取子线程消息
    private void getAsynk() {
        handler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message message) {
                switch (message.what) {
                    case 1:
                        refresh();
                        Toast.makeText(ContactBlackListActivity.this, "该好友已成功移除黑名单", Toast.LENGTH_SHORT).show();
                        break;
                }
                return false;
            }
        });
    }

    @OnItemLongClick(R.id.groupBlackListView)
    boolean moveUser(final int position, View view) {
        PopupMenu popupMenu = new PopupMenu(this, view);
        popupMenu.getMenuInflater().inflate(R.menu.moveout_black_listuser, popupMenu.getMenu());
        popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem menuItem) {
                switch (menuItem.getItemId()) {
                    case R.id.moveOut:
                        moveOut(adapter.getItem(position));
                        break;
                }
                return true;
            }
        });
        popupMenu.show();
        return false;
    }

    //获取黑名单
    private void getBlackList() {
        blackList = EMContactManager.getInstance().getBlackListUsernames();
    }

    //刷新方法
    private void refresh() {
        getBlackList();
        adapter.refreshData(blackList);
    }

    //移除黑名单中成员方法
    private void moveOut(final String name) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    EMContactManager.getInstance().deleteUserFromBlackList(name);//需异步处理
                    handler.sendEmptyMessage(1);
                } catch (EaseMobException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    public class ListAdapter extends BaseAdapter {
        private List<String> data;

        public ListAdapter(List<String> blackList) {
            data = blackList;
        }

        @Override
        public int getCount() {
            return data != null && data.size() > 0 ? data.size() : 0;
        }

        @Override
        public String getItem(int i) {
            return data != null && data.size() > 0 ? data.get(i) : null;
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            ViewHolder holder = null;
            String name = getItem(i);
            if (view == null) {
                view = LayoutInflater.from(ContactBlackListActivity.this).inflate(R.layout.group_blacklist_item, null);
                holder = new ViewHolder(view);
                view.setTag(holder);
            } else {
                holder = (ViewHolder) view.getTag();
            }
            holder.groupBlackListName.setText(name);
            GetUserAvatarHelper helper = new GetUserAvatarHelper(ContactBlackListActivity.this, holder.groupBlackPic, name);
            helper.execute(name);
            return view;
        }

        class ViewHolder {
            @Bind(R.id.groupBlackPic)
            CircleImageView groupBlackPic;
            @Bind(R.id.groupBlackListName)
            TextView groupBlackListName;

            ViewHolder(View view) {

                ButterKnife.bind(this, view);
            }
        }

        //刷新数据的方法
        public void refreshData(List<String> blackList) {
            this.data = blackList;
            notifyDataSetChanged();
        }
    }
}
